<?php
/**
 * Created by PhpStorm.
 * User: Powerless
 * Date: 2017-7-5
 * Time: 17:51
 */

namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use think\Validate;
use qcos\Cos;

class StarController extends AdminBaseController
{
    public function index()
    {
        $param    = $this->request->param();
        $s_name = isset($param['s_name']) ? $param['s_name'] : '';
        $where = null;
        if($s_name){ $where['sl.s_name'] = ['like', "%$s_name%"];}else{ unset($param['s_name']);}
        $star = Db::name('star_list')->field('sl.id,sl.s_udid,sl.s_name,sl.s_booked,sl.s_price,sl.s_ratio,ud.u_balance,sum(if(d.d_amount,d_amount,0)) as d_amount')
            ->alias('sl')
            ->join('users_detail ud','sl.s_udid = ud.id')
            ->join('deduction d','sl.s_udid = d.d_udid','LEFT')
            ->where($where)
            ->group('sl.s_udid')
            ->order(['sl.id' => 'DESC'])
            ->paginate(20);
        $star->appends($param);
        $page = $star->render();
        $this->assign("s_name", $s_name);
        $this->assign("star", $star);
        $this->assign("page", $page);
        return $this->fetch();
    }

    public function users()
    {
        $param    = $this->request->param();
        $u_nickname = isset($param['u_nickname']) ? $param['u_nickname'] : '';
        $u_phone = isset($param['u_phone']) ? $param['u_phone'] : '';
        $where['u_type'] = 0;
        if($u_nickname){ $where['u_nickname'] = ['like', "%$u_nickname%"];}else{ unset($param['u_nickname']);}
        if($u_phone){ $where['u_phone'] = ['like', "%$u_phone%"];}else{ unset($param['u_phone']);}
        $user = Db::name('users_detail')->where($where)->order(['id' => 'DESC'])->paginate(5);
        $user->appends($param);
        $page = $user->render();
        $this->assign("u_nickname", $u_nickname);
        $this->assign("u_phone", $u_phone);
        $this->assign("user", $user);
        $this->assign("page", $page);
        return $this->fetch();
    }

    public function add()
    {
        $this->assign("simgs", HTTP_IMG.VERSION_LIVESTAR.'/Default/');
        return $this->fetch();
    }

    public function addPost()
    {
        if ($this->request->isPost()) {
            $validate = new Validate([
                's_udid' => 'require|number|unique:star_list,s_udid',
                's_name' => 'require|chsAlpha|max:8|unique:star_list,s_name',
                's_price' => 'require|number|between:0,1000',
                's_ratio' => 'require|number|between:0,100',
                's_language' => 'require|chs|max:4',
                's_astro' => 'require|chs|max:3',
                's_born' => 'require|chsAlpha|max:16',
                's_agency' => 'require|chsAlpha|max:16',
                's_musictype' => 'require|chsDash|max:16',
                's_debut' => 'require|number|between:1970,2020',
                's_works' => 'require|max:64',
                's_record' => 'require|max:128',
                'path1' => 'require',
                'file1' => 'require',
                'path2' => 'require',
                'file2' => 'require',
            ]);
            $validate->message([
                's_udid.require' => '请选择用户',
                's_udid.number' => '所选用户ID错误',
                's_udid.unique' => '所选用户已使用',
                's_name.require' => '姓名不可为空',
                's_name.chsAlpha' => '姓名只能是汉字或字母',
                's_name.max' => '最大姓名长度为8个汉字或字母',
                's_name.unique' => '姓名已存在',
                's_price.require' => '直播金额不可为空',
                's_price.number' => '直播金额只能是数字',
                's_price.between' => '直播金额只能在0~1000之间',
                's_ratio.require' => '分成比例不可为空',
                's_ratio.number' => '分成比例只能是数字',
                's_ratio.between' => '分成比例只能在0~100之间',
                's_language.require' => '语言不可为空',
                's_language.chs' => '语言只能是汉字',
                's_language.max' => '语言最大长度为4个汉字',
                's_astro.require' => '星座不可为空',
                's_astro.chs' => '星座只能是汉字',
                's_astro.max' => '星座最大长度为3个汉字',
                's_born.require' => '出生地不可为空',
                's_born.chsAlpha' => '出生地只能是汉字、字母',
                's_born.max' => '出生地最大长度为16个汉字、字母',
                's_agency.require' => '经济公司不可为空',
                's_agency.chsAlpha' => '经济公司只能是汉字或字母',
                's_musictype.require' => '音乐类型不可为空',
                's_musictype.chsDash' => '音乐类型只能是汉字、字母、数字和下划线_及破折号-',
                's_musictype.max' => '音乐类型最大长度为16个汉字、字母、数字和下划线_及破折号-',
                's_debut.require' => '出道时间不可为空',
                's_debut.number' => '出道时间只能是数字',
                's_debut.between' => '出道时间在1970年~2020年之间',
                's_works.require' => '代表作品不可为空',
                's_works.max' => '代表作品最大长度为64个汉字、字母',
                's_record.require' => '主要成就不可为空',
                's_record.max' => '主要成就最大长度为128个汉字、字母',
                'path1.require' => '明星的头像不可为空',
                'file1.require' => '明星的头像不可为空',
                'path2.require' => '明星的头像不可为空',
                'file2.require' => '明星的头像不可为空',
            ]);
            $data = $this->request->post();
            if ($validate->check($data)) {

                $path1 = $data['path1'];
                $file1 = $data['file1'];
                if($path1 && $file1){
                    $data['s_image'] = $this->moveImg($path1,$file1,2);
                }
                $path2 = $data['path2'];
                $file2 = $data['file2'];
                if($path2 && $file2){
                    $data['s_backdrop'] = $this->moveImg($path2,$file2,2);
                }
                unset($data['file']);
                unset($data['path1']);
                unset($data['file1']);
                unset($data['path2']);
                unset($data['file2']);
                Db::name('star_list')->insert($data);
                Db::name('users_detail')->where(['id'=>$data['s_udid']])->update(['u_type'=>100]);
                $this->success('添加成功',url('star/index'));
            }else{
                $this->error($validate->getError());
            }
        }else{
            $this->error('请求错误');
        }
    }

    public function edit()
    {
        $id    = $this->request->param('id', 0, 'intval');
        $star = DB::name('star_list')->where(["id" => $id])->find();
        $this->assign("star", $star);
        $this->assign("simgs", HTTP_IMG.VERSION_LIVESTAR.'/Default/');
        return $this->fetch();
    }

    public function editPost()
    {
        if ($this->request->isPost()) {
            $validate = new Validate([
                's_name' => 'require|chsAlpha|max:8',
                's_price' => 'require|number|between:0,1000',
                's_ratio' => 'require|number|between:0,100',
                's_language' => 'require|chs|max:4',
                's_astro' => 'require|chs|max:3',
                's_born' => 'require|chsAlpha|max:16',
                's_agency' => 'require|chsAlpha|max:16',
                's_musictype' => 'require|chsDash|max:16',
                's_debut' => 'require|number|between:1970,2020',
                's_works' => 'require|max:64',
                's_record' => 'require|max:128',
            ]);
            $validate->message([
                's_name.require' => '姓名不可为空',
                's_name.chsAlpha' => '姓名只能是汉字或字母',
                's_name.max' => '最大姓名长度为8个汉字或字母',
                's_price.require' => '直播金额不可为空',
                's_price.number' => '直播金额只能是数字',
                's_price.between' => '直播金额只能在0~1000之间',
                's_ratio.require' => '分成比例不可为空',
                's_ratio.number' => '分成比例只能是数字',
                's_ratio.between' => '分成比例只能在0~100之间',
                's_language.require' => '语言不可为空',
                's_language.chs' => '语言只能是汉字',
                's_language.max' => '语言最大长度为4个汉字',
                's_astro.require' => '星座不可为空',
                's_astro.chs' => '星座只能是汉字',
                's_astro.max' => '星座最大长度为3个汉字',
                's_born.require' => '出生地不可为空',
                's_born.chsAlpha' => '出生地只能是汉字、字母',
                's_born.max' => '出生地最大长度为16个汉字、字母',
                's_agency.require' => '经济公司不可为空',
                's_agency.chsAlpha' => '经济公司只能是汉字或字母',
                's_musictype.require' => '音乐类型不可为空',
                's_musictype.chsDash' => '音乐类型只能是汉字、字母、数字和下划线_及破折号-',
                's_musictype.max' => '音乐类型最大长度为16个汉字、字母、数字和下划线_及破折号-',
                's_debut.require' => '出道时间不可为空',
                's_debut.number' => '出道时间只能是数字',
                's_debut.between' => '出道时间在1970年~2020年之间',
                's_works.require' => '代表作品不可为空',
                's_works.max' => '代表作品最大长度为64个汉字、字母',
                's_record.require' => '主要成就不可为空',
                's_record.max' => '主要成就最大长度为128个汉字、字母',
            ]);
            $data = $this->request->post();
            if ($validate->check($data)) {
                $path1 = $data['path1'];
                $file1 = $data['file1'];
                if($path1 && $file1){
                    $imgUrl = parse_url($data['s_image']);
                    $hostUrl = parse_url(HTTP_IMG);
                    if(!empty($imgUrl['host']) && !empty($hostUrl['host']) && ($imgUrl['host'] == $hostUrl['host'])){
                        $this->deleteImg($imgUrl['path']);
                    }
                    $data['s_image'] = $this->moveImg($path1,$file1,2);
                }
                $path2 = $data['path2'];
                $file2 = $data['file2'];
                if($path2 && $file2){
                    $imgUrl = parse_url($data['s_backdrop']);
                    $hostUrl = parse_url(HTTP_IMG);
                    if(!empty($imgUrl['host']) && !empty($hostUrl['host']) && ($imgUrl['host'] == $hostUrl['host'])){
                        $this->deleteImg($imgUrl['path']);
                    }
                    $data['s_backdrop'] = $this->moveImg($path2,$file2,2);
                }
                unset($data['file']);
                unset($data['path1']);
                unset($data['file1']);
                unset($data['path2']);
                unset($data['file2']);
                Db::name('star_list')->update($data);
                $this->success('修改成功',url('star/index'));
            }else{
                $this->error($validate->getError());
            }
        }else{ 
            $this->error('请求错误');
        }

    }

    public function uploadImg()
    {
        $file = $_FILES['file'];
        $root = VERSION_LIVESTAR.'/Default/';
        $pathName['path'] = date('Y/m/');
        $pathName['name'] = microtime (true)*10000;
        $path = $root.$pathName['path'];
        $cos = new Cos();
        $type = pathinfo($file["name"]);
        $info = $cos->upload($file["tmp_name"],$path.$pathName['name'].'.'.$type['extension']);
        if($info){
            $data['code'] = 1;
            $data['msg'] = '上传成功';
            $data['data'] = ['path' => $pathName['path'],'file' => $pathName['name'].'.'.$type['extension']];
        }else{
            $data['code'] = 0;
            $data['msg'] = '上传错误';
            $data['data'] = [];
        }
        return json_encode($data);
    }

    public function withdraw()
    {
        $param    = $this->request->param();
        $deduction = Db::name('deduction')
            ->field('d.id,sl.s_name,d.d_name,d.d_account,d.d_amount,d.d_phone,d.d_createtime,d.d_image')
            ->alias('d')
            ->join('star_list sl','d.d_udid = sl.s_udid')
            ->where(['d_udid'=>$param['id']])
            ->order(['id' => 'DESC'])
            ->paginate(20);
        $deduction->appends($param);
        $page = $deduction->render();
        $this->assign("deduction", $deduction);
        $this->assign("page", $page);
        return $this->fetch();
    }

    public function deduction()
    {
        $id    = $this->request->param('id', 0, 'intval');
        $star = DB::name('star_list')->where(['id' => $id])->find();
        $this->assign("simgs", HTTP_IMG.VERSION_LIVESTAR.'/Default/');
        $this->assign("star", $star);
        return $this->fetch();
    }

    public function deductionPost()
    {
        if ($this->request->isPost()) {
            $validate = new Validate([
                'd_udid' => 'require|number',
                'd_name' => 'require|chsAlpha|min:4|max:12',
                'd_account' => 'require|number|min:14|max:18',
                'd_phone' => 'require|number|max:11',
                'd_amount' => 'require|number',
                'd_description' => 'chsAlpha|max:128',
                'path' => 'require',
                'file' => 'require',
            ]);
            $validate->message([
                'd_udid.require' => '请选择收款明星',
                'd_udid.number' => '当前明星错误',
                'd_name.require' => '银行名称不可为空',
                'd_name.chsAlpha' => '银行名称只能是汉字或字母',
                'd_name.min' => '银行卡号最小长度为4个汉字或字母',
                'd_name.max' => '银行卡号最大长度为12个汉字或字母',
                'd_account.require' => '银行卡号不可为空',
                'd_account.number' => '银行卡号只能是数字',
                'd_account.max' => '银行卡号最大长度为18个数字',
                'd_account.min' => '银行卡号最小长度为14个数字',
                'd_phone.require' => '手机号不可为空',
                'd_phone.number' => '手机号只能是数字',
                'd_phone.max' => '银行卡号最大长度为11个数字',
                'd_amount.require' => '打款金额不可为空',
                'd_amount.number' => '打款金额只能是数字',
                'd_description.chsAlpha' => '备注只能是汉字或字母',
                'd_description.max' => '备注最大长度为128个汉字或字母',
                'path.require' => '打款截图不可为空',
                'file.require' => '打款截图不可为空',
            ]);
            $data = $this->request->post();
            if ($validate->check($data)) {
                $detail = Db::name('users_detail')->where(['id'=>$data['d_udid']])->find();
                if($detail['u_balance'] < $data['d_amount']){
                    $this->error('当前明星账户余额不足');
                }
                $path = $data['path'];
                $file = $data['file'];
                if($path && $file){
                    $data['d_image'] = $this->moveImg($path,$file,5);
                }
                unset($data['file']);
                unset($data['path']);
                $data['d_userid'] = cmf_get_current_admin_id();
                $udid = $data['d_udid'];
                $amount = $data['d_amount'];
                Db::name('users_detail')->where(['id'=>$udid])->setDec('u_balance',$amount);
                Db::name('deduction')->insert($data);
                $this->success('扣款成功',url('star/index'));
            }else{
                $this->error($validate->getError());
            }
        }else{
            $this->error('请求错误');
        }
    }
}